package com.example.recycler.write.UiActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.recycler.write.R;
import com.example.recycler.write.UiFragment.FirstFragment;
import com.example.recycler.write.UiFragment.FourthFragment;
import com.example.recycler.write.UiFragment.MyFragmentAdapter;
import com.example.recycler.write.UiFragment.SecondFragment;
import com.example.recycler.write.UiFragment.ThridFragment;
import com.example.recycler.write.Utils.UtilAnimatorString;

public class MainActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private TextView mTv1;
    private TextView mTv2;
    private TextView mTv3;
    private TextView mTv4;
    private TextView mDesTv;
    private ViewPager mVpCount;
    private int mLastPos;
    private int width;
    private int desParams;
    private FirstFragment mFirstFragment = new FirstFragment();
    private SecondFragment mSecondFragment = new SecondFragment();
    private ThridFragment mThridFragment = new ThridFragment();
    private FourthFragment mFourthFragment = new FourthFragment();
    private Fragment[] mFragments = {mFirstFragment, mSecondFragment, mThridFragment, mFourthFragment};
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置状态栏透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //设置导航栏透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        setContentView(R.layout.activity_main);
        WindowManager manager=this.getWindowManager();

        mFragmentManager = this.getSupportFragmentManager();

        mTv1 = (TextView) findViewById(R.id.tv1);
        mTv2 = (TextView) findViewById(R.id.tv2);
        mTv3 = (TextView) findViewById(R.id.tv3);
        mTv4 = (TextView) findViewById(R.id.tv4);
        mVpCount = (ViewPager) findViewById(R.id.vp_count);
        mDesTv = (TextView) findViewById(R.id.des_tv);
        ViewGroup.LayoutParams layoutParams = mDesTv.getLayoutParams();
        width = manager.getDefaultDisplay().getWidth();
        desParams=width/4;
        layoutParams.width=desParams;
        mDesTv.setLayoutParams(layoutParams);

        mTv1.setOnClickListener(this);
        mTv2.setOnClickListener(this);
        mTv3.setOnClickListener(this);
        mTv4.setOnClickListener(this);
        mVpCount.setOnPageChangeListener(this);
        mTv1.setTag(0);
        mTv2.setTag(1);
        mTv3.setTag(2);
        mTv4.setTag(3);
        MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(mFragmentManager, mFragments);
        //ViewPager上来设置预加载上限
        mVpCount.setCurrentItem(1);
        mVpCount.setAdapter(myFragmentAdapter);

    }

    @Override
    public void onClick(View v) {
        int pos = 0;
        dotranslate((TextView) v);
    }

    private void dotranslate(final TextView v) {
        final int pos = (int) v.getTag();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mDesTv, UtilAnimatorString.translationX,mLastPos*desParams,pos*desParams);
        objectAnimator.setDuration(50);
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mLastPos=pos;
                mDesTv.setText(v.getText());
                System.out.println(v.getText()+"111111111111111111");
            }
        });
        objectAnimator.start();
        mVpCount.setCurrentItem(pos);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                dotranslate(mTv1);
                break;
            case 1:
                dotranslate(mTv2);
                break;
            case 2:
                dotranslate(mTv3);
                break;
            case 3:
                dotranslate(mTv4);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
